#if !defined(_DBFCURSORSET_H__INCLUDED_)
#define _DBFCURSORSET_H__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <afxtempl.h>
#include "DBFRecordset.h"

// CDBFCursorset - the result of a query or base table browse

class TDBFUTILITY_IMPEXP CDBFCursorset : public CDBFRecordset
{
	DECLARE_DYNAMIC(CDBFCursorset)

// Constructor
public:
	CDBFCursorset(CDBFTableDef* pDBFTable = NULL);

	void Open(LPCTSTR lpszFileName, UINT nOpenFlag = TG_DBF_USE_SHARED);
	void Close();

// Attributes
public:
	BOOL IsOpen() const;
	BOOL IsBOF() const;
	BOOL IsEOF() const;

	long GetRecordCount();

protected:
	long m_nCurCursorRecN;

	BOOL m_bBOF;			
	BOOL m_bEOF;

// Operations
public:
	// Cursor operations
	void MoveNext();
	void MovePrev();
	void MoveFirst();
	void MoveLast();
	void Move(long lOffsetRec);

	long GetAbsolutePosition();
	void SetAbsolutePosition(long lPosition);

	// edit buffer operations
	virtual void AddNew();
	virtual void Edit();

	virtual void Update();
	virtual void CancelUpdate();

	virtual void Remove();

	// Recordset operations
	void Requery();

	void SetDelete(BOOL bHideDeleteRec = TRUE);
	BOOL GetDelete();

	void SetFilter(BOOL (FAR * lpfnIsInclude) (DBF_CHAR*, CDBFFields* pDBFFields));

	void SortByField(int nIndex, BOOL bDesc = FALSE);
	void SortByField(const char* lpszName, BOOL bDesc = FALSE);

// Implementation
public:
	virtual ~CDBFCursorset();

#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif


protected:
	virtual void ThrowDBFException(long nError = NO_TG_DBF_ERROR);

private:
	void SetRecord(long lRecNumber, const DBF_CHAR* lpBuffer);

	void qsort (int from, int to);
	int compare(const long elem1, const long elem2 );

	CArray<long, long> m_RecPtr;

	int m_nSortedField;
	BOOL m_bDesc;
	BOOL m_bShowDeletedRec;
	BOOL (FAR * m_lpfnIsInclude) (DBF_CHAR*, CDBFFields* pDBFFields);
};

//{{AFX_INSERT_LOCATION}}
#endif // !defined(_DBFCURSORSET_H__INCLUDED_)
